﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.ORG.DB;
using FastExecutor.ORG.Entity;
using FastIOC.Annotation;
using FastORM.Applicaiton;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{

    [RouteArea("Frame")]
    [RoutePrefix("CodeMain")]
    public class CodeMainController : FastTemplateController<Frame_CodeMain>
    {
        [AutoWired]
        private DB_CodeItem @DB_CodeItem;

        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/Code/CodeMain_List.cshtml");
        }
        [Route("Add")]
        public ActionResult Add()
        {
            return View("~/Views/Frame/Code/CodeMain_Add.cshtml");
        }
        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/Code/CodeMain_Edit.cshtml");
        }

        [HttpPost, Route("FindAll")]
        public override ActionResult FindAll()
        {
            return base.FindAll();
        }

        [HttpPost, Route("FindList")]
        public override ActionResult FindList()
        {
            JObject Params = GetParams();
            string GroupGuid = Convert.ToString(Params["GroupGuid"]);
            string CodeName = Convert.ToString(Params["CodeName"]);
            if (!string.IsNullOrEmpty(CodeName))
            {
                ListWhereText += string.Format(" and CodeName like '%{0}%'", CodeName);
            }
            if (!string.IsNullOrEmpty(GroupGuid))
            {
                ListWhereText += string.Format(" and GroupGuid='{0}'", GroupGuid);
            }
            return base.FindList();
        }

        [HttpPost, Route("FindDetail")]
        public override ActionResult FindDetail()
        {
            return base.FindDetail();
        }

        [HttpPost, Route("OperateAdd")]
        public override ActionResult OperateAdd(Frame_CodeMain Entity)
        {
            ReturnValue result = new ReturnValue();
            DBRow row = new DBRow("Frame_CodeMain", "CodeName", Entity.CodeName);
            if (row.IsExist())
            {
                result.Add("code", -100);
                result.Add("msg", "已存在相同名称的代码项！");
                return result;
            }
            else
            {
                return base.OperateAdd(Entity);
            }

        }

        [HttpPost, Route("OperateUpdate")]
        public override ActionResult OperateUpdate()
        {
            return base.OperateUpdate();
        }

        [HttpPost, Route("OperateBatchDelete")]
        public override ActionResult OperateBatchDelete()
        {
            return base.OperateBatchDelete();
        }

        [HttpPost, Route("GetCodeDic")]
        public ActionResult GetCodeDic()
        {
            ReturnValue returnvalue = new ReturnValue();
            JObject Params = GetParams();
            string CodeName = Convert.ToString(Params["CodeName"]);
            returnvalue.Add("data", @DB_CodeItem.FindCodeItemList(CodeName));
            return returnvalue;
        }
    }


}
